<?php

namespace Acme\DemoBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Acme\DemoBundle\Entity\Nhanvienthidua;

class XetThiDuaController extends Controller
{
	public function getListPhongBan(){
		return $this->getDoctrine()
		->getRepository('AcmeDemoBundle:PhongBan')->findAllByCoditions();
	}
	public function danhsachAction()
	{

		$co = "0";
		$phongbanid = 0;
		$lstphongban = $this->getListPhongBan();

		$request = $this->get('request');
		if ($request->isMethod('POST')) {
			$phongbanid = $request->request->get('phongban');
			$year = $request->request->get('year');

			$lstNhanVien = $this->getDoctrine()
			->getRepository('AcmeDemoBundle:NhanVien')
			->findByPhongBanId($phongbanid);

			$lstThiDua = array();
			foreach ($lstNhanVien as $nhanvien) {
				$thiDua = $this->getDoctrine()
				->getRepository('AcmeDemoBundle:Nhanvienthidua')
				->getByCondition($nhanvien->getId(),$year);
				if($thiDua) {
					array_push($lstThiDua,$thiDua);
				}
			}
			if(\Count($lstThiDua)>0){
				$co = "1";
			}

		} else {
			$dateNow = new \DateTime('now');
			$yearNow = intval($dateNow->format("Y"));
			$phongbanid = $lstphongban[0]->getId();
			$year = $yearNow;
			$lstNhanVien = $this->getDoctrine()
			->getRepository('AcmeDemoBundle:NhanVien')
			->findByPhongBanId($phongbanid);
			$lstThiDua = array();
			foreach ($lstNhanVien as $nhanvien) {
				$thiDua = $this->getDoctrine()
				->getRepository('AcmeDemoBundle:Nhanvienthidua')
				->getByCondition($nhanvien->getId(),$year);
				if($thiDua) {
					array_push($lstThiDua,$thiDua);
				}
			}
			if(\Count($lstThiDua)>0){
				$co = "1";
			}
			
		}
		return $this->render(
				'AcmeDemoBundle:Demo:Xetthidua.html.twig',
				array('lstphongban' => $lstphongban,
						'phongbanid' => $phongbanid,
						'lstNhanVien' => $lstNhanVien,
						'lstThiDua' => $lstThiDua,
						'year'=>$year,
						'co' =>$co

				)
		);
	}
	public function thiduaAction()
	{
		$lstphongban = $this->getListPhongBan();
		$co = "0";
		$request = $this->get('request');
		$phongbanid = $request->request->get('phongban');
		$year = $request->request->get('year');
		$action = $request->request->get('action');
			
		$lstNhanVien = $this->getDoctrine()
		->getRepository('AcmeDemoBundle:NhanVien')
		->findByPhongBanId($phongbanid);
		
		$lstThiDua = array();
		if($action == "insert"){
			
			foreach ($lstNhanVien as $nhanvien) {
				$nhanVienThiDua = new Nhanvienthidua();
				$nhanVienThiDua->setNam($year);
				$nhanVienThiDua->setCreated(new \DateTime('now'));
				$nhanVienThiDua->setNhanvienid($nhanvien);
				$thiduaid = $request->request->get($nhanvien->getId());
				$thidua = $this->getDoctrine()
				->getRepository('AcmeDemoBundle:ThiDua')
				->findOneById($thiduaid);
				$nhanVienThiDua->setThiduaid($thidua);
				array_push($lstThiDua,$nhanVienThiDua);

			}
			
		}
		else {
			foreach ($lstNhanVien as $nhanvien) {
				$nhanVienThiDua = $this->getDoctrine()
				->getRepository('AcmeDemoBundle:Nhanvienthidua')
				->getByCondition($nhanvien->getId(),$year);
				
				if($nhanVienThiDua) {
					$thiduaid = $request->request->get($nhanvien->getId());
					$thidua = $this->getDoctrine()
					->getRepository('AcmeDemoBundle:ThiDua')
					->findOneById($thiduaid);
					$nhanVienThiDua->setThiduaid($thidua);
					$nhanVienThiDua->setUpdated(new \DateTime('now'));
					array_push($lstThiDua,$nhanVienThiDua);
				}
			}
		}
		$this->getDoctrine()
		->getRepository('AcmeDemoBundle:Nhanvienthidua')
		->saveNhanvienthidua($lstThiDua);
		if(\Count($lstThiDua)>0){
			$co = "1";
		}
		return $this->render(
				'AcmeDemoBundle:Demo:Xetthidua.html.twig',
				array('lstphongban' => $lstphongban,
						'phongbanid' => $phongbanid,
						'lstNhanVien' => $lstNhanVien,
						'lstThiDua' => $lstThiDua,
						'year'=>$year,
						'co' =>$co

				)
		);
	}

}
